//
//  ImagePickerEffect.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/4/25.
//

import Foundation
import UIKit

class ImagePickerEffect: UIControl {
//    var imgs: [UIImage]? {
//        didSet {
//            imgPicker.images = imgs ?? []
//        }
//    }
    
    var imgs: [UIImage] {
        return imgPicker.images
    }
    
    var isRequired: Bool = false {
        didSet {
            updatePlaceholder()
        }
    }
    
    var placeholder: String = "..." {
        didSet {
            updatePlaceholder()
        }
    }
    
    var dataSource: [LabelDictModal] = []

    let keyL = UILabel()
    init(withPlaceholder _place: String) {
        super.init(frame: .zero)
        self.placeholder = _place
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updatePlaceholder() {
        keyL.text = placeholder
    }

    func setupUI() {
        backgroundColor = .white
        layer.borderColor = UIColor.hex("#E1E1E1").cgColor
        layer.borderWidth = 0.5
        layer.cornerRadius = 10
        
        keyL.text = placeholder
        keyL.textColor = .hex("#666666")
        keyL.font = UIFont.systemFont(ofSize: 14)
        addSubview(keyL)
        keyL.snp.makeConstraints { make in
            make.top.equalTo(self.snp.top).offset(16)
            make.left.equalTo(self.snp.left).offset(11)
        }
        
        addSubview(imgPicker)
        imgPicker.snp.makeConstraints { make in
            make.top.equalTo(keyL.snp.top)
            make.left.equalTo(keyL.snp.right).offset(20)
            make.right.equalTo(self.snp.right).offset(-10)
            make.bottom.equalTo(self.snp.bottom)
        }
    }
    
    lazy var imgPicker: BSImagePickerView = {
        let height = (ScreenWidth - 180.0) / 3.0
        let picker = BSImagePickerView()
        picker.uploadType = .photoAndCamera
        picker.key = ""
        picker.rowCount = 3
        picker.maxCount = 6
        picker.itemHeight = height
//        picker.didSelectImagesWith = {(imgs) -> () in
//            self.imgs = imgs
//        }
        
        picker.collectionView.snp.remakeConstraints { (make) in
            make.centerX.left.right.bottom.equalToSuperview()
            make.top.equalToSuperview().offset(-10)
            make.height.equalTo(height + 20)
        }
        return picker
    }()
}
